package model;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.ShortMessage;

import library.Cue;
/**All information is passed here and parsed into the proper 2 byte MIDI message**/
public class ExpressQ extends Cue{

	private ShortMessage message;
	private int qNum;
	
		public ExpressQ(String n)
		{
			super(n);
			message = new ShortMessage();
		}
		public ShortMessage getMessage()
		{
			return message;
		}
		public int getQNum()
		{
			return qNum;
		}
		public void setMessage(int cue) throws InvalidMidiDataException
		{
			qNum = cue;
			int[] data = parseQ(cue);
			message.setMessage(data[0], data[1], data[2]);
		}
		
		
		protected int[] parseQ(int cue) throws InvalidMidiDataException
		{
			if(cue <= 0 || cue >= 1000)
				throw new InvalidMidiDataException("The Express 48/96 only accepts cues between 1 and 999, inclusive.");
			int[] data = new int[3];
			if(cue <= 127) {
				data[0] = ShortMessage.PROGRAM_CHANGE;
				data[1] = cue;
				return data;
			} else if(cue <= 255) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 70;
				data[2] = cue-128;
			} else if(cue <= 383) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 71;
				data[2] = cue-256;
			} else if(cue <= 511) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 72;
				data[2] = cue-384;
			} else if(cue <= 639) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 73;
				data[2] = cue-512;
			} else if(cue <= 767) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 74;
				data[2] = cue-640;
			} else if(cue <= 895) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 75;
				data[2] = cue-768;
			} else if(cue <= 999) {
				data[0] = ShortMessage.CONTROL_CHANGE;
				data[1] = 76;
				data[2]	= cue-896;
			}
			return data;
		}	
}




